import { request } from '../request';

/**
 * 用户登录
 *
 * @param userName 用户名
 * @param password 密码
 */
export function fetchLogin(userName: string, password: string) {
  return request<Api.Auth.UserInfo>({
    url: '/api/user/login.do',
    method: 'post',
    data: {
      userName,
      password
    }
  });
}

/**
 * 用户注册
 *
 * @param username 用户名
 * @param password 密码
 * @param phoneNumber 手机号
 */
export function fetchRegister(username: string, password: string, phoneNumber: string) {
  return request<Api.Auth.UserInfo>({
    url: '/api/user/register.do',
    method: 'post',
    data: {
      username,
      password,
      phoneNumber
    }
  });
}

/**
 * 重置密码
 *
 * @param phoneNumber 手机号
 * @param username 用户名
 * @param newPassword 新密码
 */
export function fetchResetPassword(phoneNumber: string, username: string, newPassword: string) {
  return request<Api.Auth.UserInfo>({
    url: '/api/user/resetPassword.do',
    method: 'post',
    data: {
      phoneNumber,
      username,
      newPassword
    }
  });
}

/** Get user info */
export function fetchGetUserInfo(userId?: string) {
  return request<Api.Auth.UserInfo>({
    url: '/api/user/info.do',
    method: 'get',
    params: userId ? { userId } : {}
  });
}

/**
 * Refresh token
 *
 * @param refreshToken Refresh token
 */
export function fetchRefreshToken(refreshToken: string) {
  return request<Api.Auth.LoginToken>({
    url: '/api/user/auth/refreshToken',
    method: 'post',
    data: {
      refreshToken
    }
  });
}

/**
 * return custom backend error
 *
 * @param code error code
 * @param msg error message
 */
export function fetchCustomBackendError(code: string, msg: string) {
  return request({ url: '/api/user/auth/error', params: { code, msg } });
}
